<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "base.Language" );

class SysLanguage {
	
	/**
	 * Translates a string into the current language
	 *
	 * @access	public
	 * @param	string $string The string to translate
	 *
	 * @return Language
	 */
	static function &getInstance() {
		static $instance;
		if (empty ( $instance )) {
			$lang = SystemSite::getLanguage ();
			// Load system
			$folder = SYS_ROOT_PATH . 'languages';
			$folder .= DS . $lang . DS . SYS_WORK_ZONE;
			$instance = new Language ( $folder, $lang );
		}
		return $instance;
	}
	
	/**
	 * Get the language String.
	 * If the input $keyWord doesn't exists in dictionary, it will return the $keyWord
	 *
	 * @param string $string key word
	 * @return string translated string
	 */
	static function _($keyWord) {
		$iParNum = func_num_args ();
		$lang = &SysLanguage::getInstance ();
		
		$strVal = $lang->_ ( $keyWord );
		if ($iParNum == 1) {
			return $strVal;
		}
		$aPar = func_get_args ();
		for ($i = 1; $i < $iParNum; $i++) {
			$moreFormat[] = "\$aPar[{$i}]";
		}
		
		$moreFormat = implode(', ', $moreFormat);
		if (SystemSite::isDebug()) {
			eval('$strVal = sprintf($strVal, '.$moreFormat.');');
		}
		else {
			@eval('$strVal = sprintf($strVal, '.$moreFormat.');');
		}
		return $strVal;
	}
	
	/**
	 * Echo the translated string.
	 *
	 * @param string $keyWord
	 */
	static function printString($keyWord) {
		echo SysLanguage::_ ( $keyWord );
	}
	
	/**
	 * Load the language file of a application
	 *
	 * @param string $app
	 */
	static function loadAppLanguage($app = null, $lang = null) {
		if (empty ( $app )) {
			$app = SystemSite::getApplicationName ();
		}
		if (empty ( $lang )) {
			$lang = SystemSite::getLanguage ();
		}
		
		// create lang folder link and load it
		// Create path to language file
		$path = SYS_APP_PATH . $app . DS . 'languages' . DS . $lang . '.' . $app . '.lang.ini';
		
		// pass path into the lang object
		$instance = &SysLanguage::getInstance ();
		$instance->loadINI ( $path );
	
	}
	
	/**
	 * Load the language file of a module
	 *
	 * @param string $langPath - Path to Language File of Module
	 */
	static function loadLanguage($langPath) {
		$instance = &SysLanguage::getInstance ();
		$instance->loadINI ( $langPath );
	}
}
?>
